package com.greenantmall.activity;

import java.util.ArrayList;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.adapter.MyAddressListAdapter;
import com.greenantmall.bean.ShippingAddr;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyAddressActivity extends BaseActivity
{

    private final static String TAG = "MyAddressActivity";
    List<ShippingAddr> list = new ArrayList<ShippingAddr>();
    MyAddressListAdapter myFavListAdapter;
    private Context mContext;
    private TextView noAddressHint;
    private ListView favlist;
    private  RelativeLayout  nodataView;

    public static final String KEY_MODE = "key_mode";
    public static final int MODE_CHOOSE = 2;
    public static final int MODE_DEFAULT = 0;
    /**
     * 默认模式
     */
    private int mode = MODE_DEFAULT;

    public int getMode()
    {
        return mode;
    }

    public void setMode(int mode)
    {
        this.mode = mode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mContext = this;
        
        SystemBarTintManager.setContentView(this, R.layout.myaddress, getResources().getColor(R.color.green));
        Intent intent = getIntent();
        if (intent != null)
        {
            Bundle bundle = intent.getExtras();
            if (bundle != null)
            {
                mode = bundle.getInt(KEY_MODE, MODE_DEFAULT);
            }
        }

        favlist = (ListView) this.findViewById(R.id.favlist);
        noAddressHint = (TextView) this.findViewById(R.id.noAddressHint);
        nodataView = (RelativeLayout) this.findViewById(R.id.nodataView);

        myFavListAdapter = new MyAddressListAdapter(list, this);
        favlist.setAdapter(myFavListAdapter);
        this.findViewById(R.id.add).setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                if (list.size() > 4)
                {
                    UtilTools.showMyToast(mContext, mContext.getString(R.string.failed_add_address));
                } else
                {

                    Intent intent = new Intent(mContext, MyAddressEditActivity.class);
                    if (mode == MODE_CHOOSE)
                    {
                        intent.putExtra(MyAddressEditActivity.KEY_MODE, MyAddressEditActivity.MODE_CHOOSE);
                        startActivityForResult(intent, 0);
                    } else
                    {
                        startActivity(intent);
                    }

                }
            }
        });

    }

    public void goBack(View view)
    {
        finish();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        RetrieveDataAsyn.getInstance().getAddressList(
            AVUser.getCurrentUser().getObjectId(), 0,
            new RetrieveDataListener()
            {

                @Override
                public void dataRetrieved(int requestId, Object data)
                {
                    List<ShippingAddr> addressList = ((List<ShippingAddr>) data);
                    list.clear();

                    noAddressHint.setVisibility(View.INVISIBLE);
                    favlist.setVisibility(View.VISIBLE);

                    if (addressList != null)
                    {
                        list.addAll(addressList);
                        if (addressList.size() == 0)
                        {
                            noAddressHint.setVisibility(View.VISIBLE);
                            favlist.setVisibility(View.INVISIBLE);
                        } else
                        {

                        }
                    } else
                    {
                        noAddressHint.setVisibility(View.VISIBLE);
                        favlist.setVisibility(View.INVISIBLE);
                    }
                    myFavListAdapter.notifyDataSetChanged();
                    nodataView.setVisibility(View.INVISIBLE);
                }
            });
    }
    
    public void refreshView(){
        noAddressHint.setVisibility(View.VISIBLE);
        favlist.setVisibility(View.INVISIBLE);  
        
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        ((MyAddressActivity) mContext).setResult(0, data);
        ((MyAddressActivity) mContext).finish();
    }

}
